package ca.camosun.bouncer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class GameOverActivity extends Activity implements OnClickListener {

	private Button resumeButton;
	private Button endButton;
	private Button highScores;

	private int theScore;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gameover);

		resumeButton = (Button) findViewById(R.id.restartGameButton);
		resumeButton.setOnClickListener(this);

		endButton = (Button) findViewById(R.id.endGameButton);
		endButton.setOnClickListener(this);

		highScores = (Button) findViewById(R.id.viewScoresButton);
		highScores.setOnClickListener(this);

		Bundle extra = getIntent().getExtras();
		theScore = extra.getInt("score");

		TextView scoreText = (TextView) findViewById(R.id.scoreText);
		scoreText.append(String.valueOf(theScore));

		Bouncer application = (Bouncer) getApplicationContext();
		application.getHighScores();
		if (application.isHighScore(theScore) == false) {
			return;
		}
		
		if (application.isTopScore(theScore)) {
			System.out.println("This is a new record!");

			TextView newRecordText = (TextView) findViewById(R.id.newRecord);
			newRecordText.setVisibility(TextView.VISIBLE);
		}

		showNameEntryDialog();
	}

	private void showNameEntryDialog() {
		AlertDialog.Builder nameEntryDialog = new AlertDialog.Builder(this);
		nameEntryDialog.setTitle("Record your score!");
		nameEntryDialog.setMessage("Enter your name:");
		final EditText textEntry = new EditText(this);
		nameEntryDialog.setView(textEntry);

		final Bouncer application = (Bouncer) getApplicationContext();
		SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		String username_preference = mySharedPreferences.getString("editusername_preference", "PlayerName");
		textEntry.setText(username_preference);

		nameEntryDialog.setPositiveButton("Ok",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						String nameEntered = textEntry.getText().toString();

						application.saveScore(nameEntered, theScore);
					}
				});

		nameEntryDialog.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		if (v.equals(resumeButton)) {
			finish();

			Intent playAgain = new Intent(this, GameActivity.class);
			playAgain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(playAgain);

			return;
		}
		if (v.equals(endButton)) {
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);

			return;
		}
		if (v.equals(highScores)) {
			Intent scores = new Intent(this, HighScoreActivity.class);
			scores.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(scores);

			return;
		}
	}

}
